/*

  dart系列之:浏览器中的舞者,用dart发送HTTP请求
  https://blog.csdn.net/superfjj/article/details/121944082

  Dart之网络请求操作
  https://www.cnblogs.com/lxlx1798/p/11134306.html

  https://blog.csdn.net/weixin_44051815/article/details/121612206


  dart系列之:浏览器中的舞者,用dart发送HTTP请求
  https://blog.csdn.net/superfjj/article/details/121944082

  Dart基础语法-网络请求
  https://blog.csdn.net/Shinnexi/article/details/100831260

  https://blog.csdn.net/Shinnexi/article/details/100831260


  Flutter-Dart异步和网络请求
  https://blog.csdn.net/a1204942755/article/details/124478317

 */

import 'dart:io';
import 'dart:convert'; //解码和编码JSON

void main() {
  _get();
  // print("执行完毕");//会先打印这句话，说明 async没有堵塞当前线程

  _post();

}

_get() async {
  var responseBody;
  var httpClient = new HttpClient();
  var requset = await httpClient.getUrl(Uri.parse("https://httpbin.org/ip"));
  var response = await requset.close();

  if(response.statusCode == 200){
    try {


      responseBody = await response.transform(utf8.decoder).join();
      print(responseBody);

      /*
        打印结果：
          {
            "origin": "13.250.233.202"
          }

     */
    } on Exception catch (e) {
      print('_get 网络异常信息: $e');
    }
  }else{
    print('_get 响应异常 $response.statusCode');

  }

}

_post() async{
  var responseBody;
  var httpClient = new HttpClient();
  var requset = await httpClient.postUrl(Uri.parse('http://www.wanandroid.com/user/login?username=1&password=123456'));
  var response = await requset.close();
  if(response.statusCode == 200){

    try{
      responseBody = await response.transform(utf8.decoder).join();
      print(responseBody);
    }on Exception catch(e){
      print('_post 网络异常信息: $e');
    }
  }else{
    print('_post 响应异常 ${response.statusCode}');
    //_post 响应异常 302  重定向到错误资源的页面

  }
}
